using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Framework;
using Framework.DataTable;
using UnityEngine;

public class GameDataTableProcessor : BuiltinProcessor
{
    public GameDataTableProcessor()
    {
        AddParser(new Vector3Ext());
        AddParser(new SceneExitData());
        SetTypeAlias(typeof(Vector3), "Vector3", "vec3", "v3");
        SetDataType("Lang", typeof(Lang1));
    }
    public override DataTableManager GetManager()
    {
        return new TableManager();
    }
    public override void BuildCode(ProcessData data)
    {
        if (data.Define == "c")
        {
            data.Path = Path.Combine(Application.dataPath, "Game/Scripts/Generate/DataTable");
            base.BuildCode(data);
        }

    }
    public override void BuildData(ProcessData data)
    {
        if (data.Define == "c")
        {
            data.Path = Path.Combine(Application.dataPath, "Res/DataTable");
            base.BuildData(data);
        }
        else if (data.Define == "s")
        {

            // foreach (var item in data.DataInfos)
            // {
            //     if (item.ClassInfo.GetFieldCount() < 1)
            //     {
            //         continue;
            //     }
            //     buildJsonData(item);
            // }

        }
    }
    private void buildJsonData(DataInfo info)
    {
        var name = info.Name;
        if (!string.IsNullOrEmpty(info.Branch))
        {
            name += "_" + info.Branch;
        }


        var builder = new StringBuilder();
        builder.AppendLine("[");
        var len = info.Count;
        for (var i = 0; i < len; i++)
        {
            if (i > 0)
            {
                builder.AppendLine(",");
            }
            var line = info.GetLine(i);
            builder.Append("{");
            for (var j = 0; j < info.ClassInfo.Fields.Count; j++)
            {
                if (j > 0)
                {
                    builder.Append(",");
                }
                var f = info.ClassInfo.Fields[j];
                builder.Append("\"" + f.Name + "\":");
                builder.Append(GetJsonValue(f.Type, line[f.DeclarIndex]));
            }
            builder.Append("}");
        }
        builder.AppendLine();
        builder.AppendLine("]");
        var path = Path.Combine(Application.temporaryCachePath, name + ".json");
        File.WriteAllText(path, builder.ToString());

    }
    private string GetJsonValue(SimpleTypeDef typeInfo, string value)
    {
        var t = typeInfo.TypeName;
        if (t == "bool")
        {
            return Utils.Parse.ParseBool(value).ToString().ToLower();
        }
        if (typeInfo.IsString)
        {
            return "\"" + value + "\"";
        }
        if (typeInfo.IsClass)
        {
            var cfg = GetParser(typeInfo.Name);
            if (cfg != null)
            {
                var obj = cfg.Deserialize(value);
                return cfg.SerializeObject(obj, "json");
            }
            else
            {
                return "";
            }
        }
        if (typeInfo.IsArray)
        {
            var lst = Utils.Parse.ParseList(value);
            if (lst == null || lst.Length < 1)
            {
                return "[]";
            }
            var len = lst.Length;
            var str = "[";
            for (var i = 0; i < len; i++)
            {
                if (i > 0)
                {
                    str += ",";
                }
                str += GetJsonValue(new SimpleTypeDef(typeInfo.TypeName), lst[i]);
            }
            str += "]";
            return str;
        }
        return value;
    }
}
